import 'package:flutter/material.dart';
import 'package:gzmall_app/common/empty_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:pull_to_refresh/src/internals/default_constants.dart';

class RefreshListView extends StatelessWidget {
  final ScrollView child;
  final IndicatorBuilder headerBuilder;
  final IndicatorBuilder footerBuilder;
  final Config headerConfig, footerConfig;
  final bool enablePullUp;
  final bool enablePullDown;
  final bool enableOverScroll;
  final OnRefresh onRefresh;
  final OnOffsetChange onOffsetChange;
  final RefreshController controller;
  final bool showEmpty;
  final String emptyTips;
  final Function onReload;
  final Widget emptyView;

  RefreshListView({
    Key key,
    @required this.child,
    IndicatorBuilder headerBuilder,
    IndicatorBuilder footerBuilder,
    RefreshController controller,
    this.headerConfig: const RefreshConfig(),
    this.footerConfig: const LoadConfig(triggerDistance: 100.0),
    this.enableOverScroll: default_enableOverScroll,
    this.enablePullDown: default_enablePullDown,
    this.enablePullUp: default_enablePullUp,
    this.onRefresh,
    this.onOffsetChange,
    this.emptyView,
    this.showEmpty: false,
    this.emptyTips: "暂无数据",
    this.onReload,
  })  : assert(child != null),
        controller = controller ?? new RefreshController(),
        this.headerBuilder = headerBuilder ??
            ((BuildContext context, int mode) {
              return new ClassicIndicator(
                mode: mode,
                releaseText: '松开刷新数据',
                refreshingText: '刷新中...',
                completeText: '刷新完成',
                noDataText: '没有更多的数据',
                failedText: '刷新失败',
                idleText: '拉动刷新',
                noMoreIcon: Container(),
              );
            }),
        this.footerBuilder = footerBuilder ??
            ((BuildContext context, int mode) {
              return new ClassicIndicator(
                mode: mode,
                releaseText: '松开刷新数据',
                refreshingText: '刷新中...',
                completeText: '刷新完成',
                noDataText: '没有更多的数据',
                failedText: '刷新失败',
                idleText: '拉动刷新',
                releaseIcon:
                    const Icon(Icons.arrow_downward, color: Colors.grey),
                idleIcon: const Icon(Icons.arrow_upward, color: Colors.grey),
                noMoreIcon: Container(),
              );
            }),
        super(key: key);

  sendBack(bool up, int mode, {bool hasMore: true}) {
    if (this.controller != null && !this.showEmpty) {
      this.controller.sendBack(up, mode);

      if (up && mode == RefreshStatus.completed && this.enablePullUp) {
        this.controller.sendBack(
            false, hasMore ? RefreshStatus.canRefresh : RefreshStatus.noMore);
      }
    }
  }

  refreshCompleted(bool up, {bool hasMore: true}) {
    sendBack(
        up,
        up
            ? RefreshStatus.completed
            : hasMore ? RefreshStatus.canRefresh : RefreshStatus.noMore,
        hasMore: hasMore);
  }

  isRefresh(bool up) {
    if (this.controller != null && !this.showEmpty) {
      return this.controller.isRefresh(up);
    }
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return !this.showEmpty
        ? SmartRefresher(
            headerBuilder: this.headerBuilder,
            footerBuilder: this.footerBuilder,
            enablePullDown: this.enablePullDown,
            enablePullUp: this.enablePullUp,
            onRefresh: this.onRefresh,
            onOffsetChange: this.onOffsetChange,
            controller: this.controller,
            footerConfig: this.footerConfig,
            headerConfig: this.headerConfig,
            child: this.child,
          )
        : this.emptyView ??
            EmptyWidget(
              tips: this.emptyTips,
              reloadAction: () {
                if (this.onReload != null) {
                  this.onReload();
                }
              },
            );
  }
}
